package com.wangdong.wdbase.base;

import android.app.Activity;
import android.content.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by luyz on 2017/7/17.
 * activity stack CLASS
 */

public class XTActivityStack {

    private static XTActivityStack instance;

    protected static XTActivityStack newInstance(){
        return new XTActivityStack();
    }

    public static XTActivityStack getInstance() {

        if (instance == null) {
            synchronized (XTActivityStack.class) {
                if (instance == null) {
                    instance = newInstance();
                }
            }
        }
        return instance;
    }

    private Stack<Activity> activityStack;

    private XTActivityStack(){
        activityStack = new Stack<>();
    }

    /**
     * add Activity 添加Activity到栈
     * @param activity  FragmentActivity
     */
    public void addActivity(Activity activity) {

        activityStack.add(activity);
    }

    /**
     * remove Activity 移除Activity
     * @param activity FragmentActivity
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
        }
    }

    /**
     *
     * 根据类名获取Acticity
     * @param cls Class
     * @return FragmentActivity
     */
    public Activity getActivity(Class<?> cls){

        Activity tempResult = null;

        for (int i=activityStack.size()-1;i>-1;i--) {
            Activity activity = activityStack.get(i);
            if (activity!=null){

                if (activity.getClass().equals(cls)) {
                    tempResult = activity;
                    break;
                }
            }
        }

        return tempResult;
    }

    /**
     * get current Activity 获取当前Activity（栈中最后一个压入的）
     */
    public Activity currentActivity() {
        return activityStack.lastElement();
    }

    /**
     * 结束当前Activity（栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            removeActivity(activity);
            activity.finish();
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {

        Activity result = null;

        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                result = activity;
                break;
            }
        }

        if (result!=null){
            finishActivity(result);
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {

        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }
    /**
     * 结束cls类以上所有页面，不包括cls类
     * @param cls Class
     */
    public void finishActivityToBottom(Class<?> cls){

        List<Activity> result = new ArrayList<>();

        boolean hasAc = false;

        for (int i=activityStack.size()-1;i>-1;i--) {
            Activity activity = activityStack.get(i);
            if (activity!=null){

                if (activity.getClass().equals(cls)) {
                    hasAc = true;
                    break;
                }
                result.add(activity);
            }
        }

        if (hasAc){
            for (Activity activity : result){
                finishActivity(activity);
            }
        }

        result.clear();
    }

    /**
     * 结束cls类以上所有页面，包括cls类
     * @param cls Class
     */
    public void finishActivityToTop(Class<?> cls){

        List<Activity> result = new ArrayList<>();

        boolean hasAc = false;

        for (int i=activityStack.size()-1;i>-1;i--) {
            Activity activity = activityStack.get(i);
            if (activity!=null){
                result.add(activity);
                if (activity.getClass().equals(cls)) {
                    hasAc = true;
                    break;
                }
            }
        }

        if (hasAc){
            for (Activity activity : result){
                finishActivity(activity);
            }
        }

        result.clear();
    }

    /**
     * 退出应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception ignored) {

        }
    }


}
